
let iconStyle = {
    success: 'success',
    error: 'error',
    info: 'info',
    warn: 'warn'
}

let $bui = {
    //消息提示
    message:function (val) {
        if (typeof val === 'string'){
            //默认提示
            message(iconStyle.info,val);
        }else {
            switch (val.type) {
                case iconStyle.success:
                    message(iconStyle.success,val.message);
                    break;
                case iconStyle.error:
                    message(iconStyle.error,val.message);
                    break;
                case iconStyle.info:
                    message(iconStyle.info,val.message);
                    break;
                case iconStyle.warn:
                    message(iconStyle.warn,val.message);
                    break;
            }
        }
    },
    msg:{
        ok: function (val) {
            message(iconStyle.success,val);
        },
        error: function (val) {
            message(iconStyle.error,val);
        },
        info: function (val) {
            message(iconStyle.info,val);
        },
        warn: function (val) {
            message(iconStyle.warn,val);
        }
    }
}

//封装
function message(icon,msg){
    addMsg(['icon-'+icon,msg]);
    msgAnimate('bl-message-'+icon);
}

//消息弹窗动画
function msgAnimate(style) {
    setTimeout(function () {
        $(".bl-message").addClass(style);
        setTimeout(function () {
            $(".bl-message").removeClass(style);
            setTimeout(function () {
                $(".bl-message").remove();
            },400)
        },3000)
    },0)
}

//添加消息框
function addMsg(date) {
    $(".bl-message").remove();
    $("body").append($(regReplace(messageTem,date)));
}

//消息组件
let messageTem = "<div class=\"bl-message\">\n" +
    "    <i class='iconfont {} message-icon'></i>\n" +
    "    <p>{}</p>\n" +
    "</div>";

//预留字符替换
function regReplace(o,s){
    for (let i = 0;i<s.length;i++){
        o = o.replace("{}",s[i])
    }
    return o;
}


/**
 * 表格模块
 * el 表格id
 * url 链接
 * queryData 查询参数
 * columns 标题与对应字段
 */
function Table(args) {
    this.el = args.el;
    this.url = args.url;
    this.queryData = args.queryData;
    this.columns = args.columns;
    this.enableOption = args.enableOption;
    this.enableUpBtn = args.enableUpBtn;
    this.enableDelBtn = args.enableDelBtn;

    let _this = this;
    $(this.el).addClass('bl-table');

    $.ajax({
        url: this.url,
        success: function (response) {
            let cols = _this.columns;
            let $head_tr = $("<tr></tr>");
            //加载表头
            for (let i=0;i<cols.length;i++){
                $head_tr.append("<th>"+ cols[i].title +"</th>");
            }
            if (_this.enableOption){
                $head_tr.append("<th>操作</th>");
            }

            $(_this.el).append($head_tr);

            let data = response.data;

            if (data===null || data.length ===undefined){
                $(_this.el).append("<tr><td colspan='"+cols.length+"' style='text-align: center'>未查询到数据</td></tr>");
                return;
            }

            for (let i=0;i<data.length;i++){
                let $tr = $("<tr></tr>");
                for (let j=0;j<cols.length;j++){
                    let field = cols[j].field;
                    $tr.append("<td>" + data[i][field] +"</td>");
                }
                if (_this.enableOption){
                    let $opTd = $("<td></td>");
                    if (_this.enableUpBtn){
                        $opTd.append("<button class=\"bl-btn bl-btn-info\" bl-size=\"mini\">修改</button>");
                    }
                    if (_this.enableDelBtn){
                        $opTd.append(" <button class=\"bl-btn bl-btn-danger\" bl-size=\"mini\">删除</button>");
                    }
                    $tr.append($opTd);
                }
                $(_this.el).append($tr);
            }
        }
    })

    this.load = function () {
        console.log('查询表格数据...')
    }
}